<?php

namespace App\Http\Controllers\Home;

use App\Model\Code;
use App\Model\Earn;
use App\Model\Tixian;
use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use QrCode;

class UserController extends Controller
{
    public function personal(){
        $user = Auth::guard('home')->user();
        if(empty($user)){
            return redirect('login');
        }
        $begin = date('Y-m-d H:i:s',strtotime(date('Y-m-d')));
        $end = date('Y-m-d H:i:s',strtotime(date('Y-m-d'))+24*60*60);
        $total = Earn::where('uid',$user->id)->sum('money');
        $tixian = Tixian::where('uid',$user->id)->sum('money');
        $today = Earn::where('uid',$user->id)->where('created_at','<',$end)->where('created_at','>',$begin)->sum('money');
        $balance = $total - $tixian;
        $data['total'] = sprintf('%.2f',$total/100);
        $data['balance'] = sprintf('%.2f',$balance/100);
        $data['today'] = sprintf('%.2f',$today/100);

        return view('home.personal.index',['user'=>$user,'data'=>$data]);
    }

    public function edit(){
        $user = Auth::guard('home')->user();

        return view('home.personal.edit',['user'=>$user]);
    }

    public function edit_post(Request $request){
        $return = array();
        do{
            $user = Auth::guard('home')->user();
            if(empty($user)){
                $return['code'] = 101;
                $return['message'] = '请重新登录';
                break;
            }
            $data['name'] = trim($request->input('name',''));
            $data['mobile'] = trim($request->input('mobile',''));
            $data['password'] = trim($request->input('password',''));
            $res = User::where('id',$user->id)->update($data);
            if(!empty($res)){
                $return['code'] = 0;
                $return['message'] = '更新成功';
                break;
            }else{
                $return['code'] = 101;
                $return['message'] = '更新失败，请重试';
                break;
            }

        }while(false);
        return json_encode($return);
    }

    public function tixian(){
        $user = Auth::guard('home')->user();
        return view('home.personal.tixian',['user'=>$user]);
    }

    public function tixian_post(Request $request){
        $return = array();
        do{
            $mobile = $request->input('mobile');
            $money = (int)$request->input('money');
            if(empty($mobile)){
                $return['code'] = 101;
                $return['message'] = '缺少参数';
                break;
            }
            if($money < 100){
                $return['code'] = 102;
                $return['message'] = '提现金额大于100元才能提取';
                break;
            }
            $user = Auth::guard('home')->user();
            $total = Earn::where('uid',$user->id)->sum('money');
            $tixian = Tixian::where('uid',$user->id)->sum('money');
            $balance = $total - $tixian;
            if($money*100 > $balance){
                $return['code'] = 103;
                $return['message'] = '余额不足';
                break;
            }

            $info = new Tixian();
            $info->uid = $request->input('uid');
            $info->mobile = $mobile;
            $info->name = trim($request->input('name'));
            $info->money = $money*100;
            $info->img = $request->input('img');
            $info->save();

            $return['code'] = 0;
            $return['message'] = '资金1-2个工作日到账，请注意查收';
            break;

        }while(false);
        return json_encode($return);
    }

    public function tixian_record(){
        $user = Auth::guard('home')->user();
        $sql = Tixian::where('mobile',$user->mobile);
        $data['total'] = $sql->sum('money');
        $data['list'] = $sql->orderby('id','desc')->get();
        return view('home.personal.tixian_record',['data'=>$data]);
    }
    public function team($id = 0){
        $user = Auth::guard('home')->user();
        $uid = $user->id;
        $list = User::where('pid',$uid)->where('deleted',0)->get();
        $res = $res1 = $res2 = [];
        foreach ($list as $k => $v){
            $info = [];
            $info['id'] = $v->id;
            $info['name'] = $v->name;
            $info['img'] = $v->img;
            array_push($res,$info);

            $list1 = User::where('pid',$v->id)->where('deleted',0)->get();
            foreach ($list1 as $k1 => $v1){
                $info1 = [];
                $info1['id'] = $v1->id;
                $info1['name'] = $v1->name;
                $info1['img'] = $v1->img;
                array_push($res1,$info1);

                $list2 = User::where('pid',$v1->id)->where('deleted',0)->get();
                foreach ($list2 as $k2 => $v2){
                    $info2 = [];
                    $info2['id'] = $v2->id;
                    $info2['name'] = $v2->name;
                    $info2['img'] = $v2->img;
                    array_push($res2,$info2);
                }
            }
        }
        if(empty($id)){
            $data['list'] = $res;
        }elseif ($id == 1){
            $data['list'] = $res1;
        }elseif ($id == 2){
            $data['list'] = $res2;
        }
        $data['count'] = count($res);
        $data['count1'] = count($res1);
        $data['count2'] = count($res2);

        return view('home.personal.team',['id'=>$id,'data'=>$data]);
    }


    public function mycard(){
        $user = Auth::guard('home')->user();
        $path = $_SERVER['DOCUMENT_ROOT'].'/uploads/qrcode';
        if(!file_exists($path)){
            mkdir($path,0777,true);
        }
        $filename = $path.'/'.$user->id.'.png';
        $url = url('reg').'?id='.$user->id;
        if(!file_exists($filename)){
            QrCode::format('png')->size(140)->color(0,0,0)->generate($url,$filename);
        }
        return view('home.personal.mycard',['user'=>$user]);
    }
    public function record(){
        $user = Auth::guard('home')->user();
        $data = Earn::where('uid',$user->id)->orderby('id','desc')->get();
        return view('home.personal.record',['data'=>$data]);
    }
    public function guide(){
        return view('home.personal.guide');
    }

    public function jihuo(){
        return view('home.personal.jihuo');
    }
    public function jihuo_post(Request $request){
        $return = array();
        do{
            $user = Auth::guard('home')->user();
            $code = $request->input('code');
            if(empty($code)){
                $return['code'] = 101;
                $return['message'] = '请输入激活码';
                break;
            }
            $info = Code::where('code',$code)->where('status',0)->where('is_can',0)->first();
            if(empty($info)){
                $return['code'] = 102;
                $return['message'] = '请输入有效激活码';
                break;
            }
            $res = User::where('id',$user->id)->update(['status'=>1]);
            if(!empty($res)){
                $info->status = 1;
                $info->save();

                $return['code'] = 0;
                $return['message'] = '激活成功';
                break;
            }else{
                $return['code'] = 103;
                $return['message'] = '激活失败，请重试';
                break;
            }

        }while(false);
        return json_encode($return);
    }
}
